#include "FWGraphic.h"


FWGraphic::FWGraphic(FWGame* fwGame)
{
	this->_gameObject = fwGame;
}

FWGraphic::~FWGraphic(void)
{
}

D3DXVECTOR2 FWGraphic::_worldPositionToViewport(D3DXVECTOR2 world,D3DXVECTOR2 center)
{
	D3DXVECTOR4 vp_pos;
	D3DXVECTOR3 _world(world.x + center.x ,world.y +  center.y,0);
	D3DXVec3Transform(&vp_pos,&_world,this->_tranform);
	return D3DXVECTOR2(vp_pos.x,vp_pos.y);
}

bool FWGraphic::init()
{
	HRESULT hr = D3DXCreateSprite(_gameObject->d3ddev,&this->_sprite);
	if(FAILED(hr)) return false;
	backbuffer = NULL;
	return true;	
}

void FWGraphic::begin()
{
	this->_gameObject->d3ddev->BeginScene();
	this->_sprite->Begin(D3DXSPRITE_ALPHABLEND);
	this->_tranform = NULL;
}

void FWGraphic::begin(D3DXMATRIX* tranform)
{

	this->_gameObject->d3ddev->BeginScene();
	this->_sprite->Begin(D3DXSPRITE_ALPHABLEND);
	this->_tranform = tranform;
}

void FWGraphic::end()
{
	this->_sprite->End();
	this->_gameObject->d3ddev->EndScene();
	this->_gameObject->d3ddev->Present(NULL,NULL,NULL,NULL);	
}

void FWGraphic::clear(D3DCOLOR color)
{
	this->_gameObject->d3ddev->Clear(1,NULL,D3DCLEAR_TARGET,color,1,NULL);
}

void FWGraphic::nDraw(FWTexture* texture,D3DXVECTOR2 position,D3DXVECTOR2 center, D3DCOLOR color)
{
	D3DXVECTOR3 _center(center.x,center.y,0);
	D3DXVECTOR3 _position(position.x,position.y,0);
	this->_sprite->Draw(texture->getTexture(),NULL,&_center,&_position,color);
}

void FWGraphic::nDraw(FWTexture* texture,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{
	//tim do dan
	float xScale = (destRect.right - destRect.left) / texture->getWidth();
	float yScale = (destRect.bottom - destRect.top) / texture->getHeight();
	//cac ma tran ve ti le
	D3DXMATRIX result,currentState;
	//tao ma tran scale
	D3DXMatrixScaling(&result,xScale,yScale,0);	
	//tao position
	D3DXVECTOR3 _position(destRect.left / xScale, destRect.top / yScale,0);
	//tao diem giua
	D3DXVECTOR3 _center(center.x / xScale, center.y / yScale,0);
	//ap dung cac transform
	_sprite->GetTransform(&currentState);
	_sprite->SetTransform(&result);
	_sprite->Draw(texture->getTexture(),NULL,&_center,&_position,color);
	//khoi phuc cac transforms
	_sprite->SetTransform(&currentState);

}

void FWGraphic::nDraw(FWTexture* texture,RECT sourRect,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{

	//tim do dan
	float xScale = (destRect.right - destRect.left) / (sourRect.right - sourRect.left);
	float yScale = (destRect.bottom - destRect.top) / (sourRect.bottom - sourRect.top);
	//cac ma tran ve ti le
	D3DXMATRIX result,currentState;
	//tao ma tran scale
	D3DXMatrixScaling(&result,xScale,yScale,0);	
	//tao position
	D3DXVECTOR3 _position(destRect.left / xScale, destRect.top / yScale,0);
	//tao diem giua
	D3DXVECTOR3 _center(center.x / xScale, center.y / yScale,0);
	//ap dung cac transform
	_sprite->GetTransform(&currentState);
	_sprite->SetTransform(&result);
	_sprite->Draw(texture->getTexture(),&sourRect,&_center,&_position,color);
	//khoi phuc cac transforms
	_sprite->SetTransform(&currentState);


}
























void FWGraphic::tDraw(FWTexture* texture,D3DXVECTOR2 position,D3DXVECTOR2 center, D3DCOLOR color)
{
	D3DXVECTOR2 _vp_pos = _worldPositionToViewport(position,center);
	this->nDraw(texture,_vp_pos,center,color);
}

void FWGraphic::tDraw(FWTexture* texture,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{
	D3DXVECTOR2 _vp_pos = this->_worldPositionToViewport(D3DXVECTOR2(destRect.left,destRect.top),center);
	int _width = destRect.right - destRect.left;
	int _height = destRect.bottom - destRect.top;
	RECT tdes;
	tdes.left = _vp_pos.x;
	tdes.top = _vp_pos.y;
	tdes.right = tdes.left + _width;
	tdes.bottom = tdes.top + _height;
	this->nDraw(texture,tdes,center,color);
}


void FWGraphic::tDraw(FWTexture* texture,RECT sourRect,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{
	D3DXVECTOR2 _vp_pos = this->_worldPositionToViewport(D3DXVECTOR2(destRect.left,destRect.top),center);
	int _width = destRect.right - destRect.left;
	int _height = destRect.bottom - destRect.top;
	RECT tdes;
	tdes.left = _vp_pos.x;
	tdes.top = _vp_pos.y;
	tdes.right = tdes.left + _width;
	tdes.bottom = tdes.top + _height;
	this->nDraw(texture,sourRect,tdes,center,color);
}
